package com.ruoyi.project.dataviewSystem.service.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import com.ruoyi.project.configureSystem.domain.VDatainfo;
import com.ruoyi.project.configureSystem.mapper.VDatainfoMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.project.dataviewSystem.mapper.CDatavalueMapper;
import com.ruoyi.project.dataviewSystem.domain.CDatavalue;
import com.ruoyi.project.dataviewSystem.service.ICDatavalueService;

/**
 * 历史数据Service业务层处理
 * 
 * @author wangshuo
 * @date 2021-01-07
 */
@Service
public class CDatavalueServiceImpl implements ICDatavalueService 
{
    @Autowired
    private CDatavalueMapper cDatavalueMapper;
    @Autowired
    private VDatainfoMapper vDatainfoMapper;

    /**
     * 查询历史数据
     * 
     * @param id 历史数据ID
     * @return 历史数据
     */
    @Override
    public CDatavalue selectCDatavalueById(Long id)
    {
        return cDatavalueMapper.selectCDatavalueById(id);
    }

    /**
     * 查询历史数据列表
     * 
     * @param cDatavalue 历史数据
     * @return 历史数据
     */
    @Override
    public List<CDatavalue> selectCDatavalueList(CDatavalue cDatavalue)
    {
        return cDatavalueMapper.selectCDatavalueList(cDatavalue);
    }

    /**
     * 新增历史数据
     * 
     * @param cDatavalue 历史数据
     * @return 结果
     */
    @Override
    public int insertCDatavalue(CDatavalue cDatavalue)
    {
        return cDatavalueMapper.insertCDatavalue(cDatavalue);
    }

    /**
     * 修改历史数据
     * 
     * @param cDatavalue 历史数据
     * @return 结果
     */
    @Override
    public int updateCDatavalue(CDatavalue cDatavalue)
    {
        return cDatavalueMapper.updateCDatavalue(cDatavalue);
    }

    /**
     * 批量删除历史数据
     * 
     * @param ids 需要删除的历史数据ID
     * @return 结果
     */
    @Override
    public int deleteCDatavalueByIds(Long[] ids)
    {
        return cDatavalueMapper.deleteCDatavalueByIds(ids);
    }

    /**
     * 删除历史数据信息
     * 
     * @param id 历史数据ID
     * @return 结果
     */
    @Override
    public int deleteCDatavalueById(Long id)
    {
        return cDatavalueMapper.deleteCDatavalueById(id);
    }

    @Override
    public List<VDatainfo> getLastDatavalueByDataItemId(String stationcode, String dataItemId) {
        String tableName = "datadb_"+stationcode;
//        String tableName = "datadb_ldks784471";
//        return cDatavalueMapper.getLastDatavalueByDataItemId(tableName,dataItemId);
        //获取dataCode
        List<VDatainfo> vDatainfos = vDatainfoMapper.selectDatainfoByDataItemId(dataItemId);
        for (VDatainfo vDatainfo : vDatainfos) {
            String datacode = vDatainfo.getDatacode();
            //先查询数据库，后面改，todo
            CDatavalue cDatavalue = null;
            try {
                cDatavalue = cDatavalueMapper.getLastDatavalueByDatacode(tableName,datacode);
            } catch (Exception e) {
                e.printStackTrace();
               cDatavalue = new CDatavalue();
            }
            if(cDatavalue!=null){
                vDatainfo.setNumValue(cDatavalue.getNumvalue());
//                vDatainfo.setTextValue(cDatavalue.getTextvalue());
            }
        }
        return vDatainfos;
    }

    @Override
    public List<CDatavalue> getDatavalueByDay(String stationcode, String datacode,Integer day) {
        String tableName = "datadb_"+stationcode;
//        String tableName = "datadb_ldks784471";
        //为了走索引，不用时间函数
        SimpleDateFormat sf = new SimpleDateFormat("HH:ss:mm");
        SimpleDateFormat sf2 = new SimpleDateFormat("yyyy-MM-dd HH:ss:mm");
        SimpleDateFormat sf3 = new SimpleDateFormat("yyyy-MM-dd HH");
        String startTime =null;
        String endTime = null;
        try {
            if(day==1){
                //当天
                startTime = sf.format(new Date())+" 00:00:00";
                endTime = sf2.format(new Date());
            }
            //小时
            if(day==2){
                startTime = sf3.format(new Date())+"00:00";
                endTime = sf3.format(new Date());
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        day =null;//暂时查询所以
        try {
            return cDatavalueMapper.getDatavalueByDay(tableName,datacode,startTime,endTime);
        } catch (Exception e) {
            e.printStackTrace();
            return new ArrayList<>();
        }
    }
}
